﻿using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	//http://www.youtube.com/watch?v=pSN2x3dPgYw&list=PLQSzy9LlXwas-6uXiU5rCoYc0mHUWykpJ&index=6
	// Handling
	float rotationSpeed = 550f;
	float walkSpeed = 6f;
	float runSpeed = 10f;
	float dragRange = 1.0f;
	bool dashing = false;
	float dashTime;
	GameObject draggingObj;
	bool dragging = false;
	CharacterJoint joint;

	//System
	private Quaternion targetRotation;
	
	private Camera cam;

	public Gun gun = new Gun ();

	void Start () {
		cam = Camera.main;
	}

	void Update (){
	    if (Input.GetButton ("Aim")){
			ControlMouse ();
		}
		else
	    {
			ControlWASD();
	    }
		if (Input.GetButton ("Shoot")) {
			//animation.Play("Sword2_Swipe");
			gun.Shoot ();
		}
		if (Time.time - dashTime > 0.2f) {
			dashing = false;
		}
		if(Input.GetButtonUp("Drag")){
			toggleDragging();
		}
	}
		
	void toggleDragging(){
		if (dragging) {
			joint.connectedBody = null;
			draggingObj = null;
			dragging = false;
		} else {
			Collider[] collide = Physics.OverlapSphere (new Vector3 (transform.position.x, transform.position.y, transform.position.z), dragRange, 1 << LayerMask.NameToLayer ("Draggable"));
			if (collide.Length > 0) {
				draggingObj = findClosestItem (collide);
				joint = draggingObj.GetComponent<CharacterJoint>();
				if(joint == null){
					joint = draggingObj.AddComponent<CharacterJoint>();
				}
				joint.connectedBody = rigidbody;
				dragging = true;
			}
		}
	}
	
	GameObject findClosestItem(Collider[] collide){
		GameObject closest = null;
		float minDist = Mathf.Infinity;
		foreach(Collider col in collide){
			float dist = Vector3.Distance(col.transform.position, transform.position);
			if(dist < minDist){
				minDist = dist;
				closest = col.gameObject;
			}
		}
		return closest;
	}

	void ControlMouse() {

		turnToMouse ();
		Vector3 input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
		moveToDir (input, walkSpeed);
}
	void ControlWASD() {
			
		Vector3 input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
			
		if (input != Vector3.zero) {
			targetRotation = Quaternion.LookRotation (input);
			transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle (transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
			moveToDir (input, runSpeed);
		} else {
			if(!dashing){
				rigidbody.velocity = Vector3.zero;
			}
			turnToMouse ();
		}
	}

	public void setDashing(){
		dashing = true;
		dashTime = Time.time;
	}

	void moveToDir(Vector3 motion, float speed){
		if (!dashing) {
			motion *= (Mathf.Abs (motion.x) == 1 && Mathf.Abs (motion.z) == 1) ? .7f : 1;
			if(dragging){
				motion *= speed/2;
			}else{
				motion *= speed;
			}
			rigidbody.velocity = motion;
		}
	}

	void turnToMouse(){
		Vector3 mousePos = Input.mousePosition;
		mousePos = cam.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
		targetRotation = Quaternion.LookRotation (mousePos - new Vector3 (transform.position.x, 0, transform.position.z));
		transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle (transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
	}
}
